package luckyNumbers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {
    public List<Integer> luckyNumbers (int[][] matrix) {
        // 思路 找到所有行的最小数和所有列的最大数
        int m = matrix.length;
        int n = matrix[0].length;
        int[] min = new int[m]; // 每行最小数的索引
        int[][] max = new int[n][2]; // 每列最大数的索引
        for (int i = 0; i < m; i++) {
            int minRow = Integer.MAX_VALUE;
            for (int j = 0; j < n; j++) {
                if(minRow>matrix[i][j]){
                    min[i] = j;
                    minRow = matrix[i][j];
                }
                if(max[j][1]<matrix[i][j]){
                    max[j][0] = i;
                    max[j][1] = matrix[i][j];
                }
            }
        }
        System.out.println(Arrays.toString(min));
        List<Integer> ans = new ArrayList<>();
        for (int i = 0; i < min.length; i++) {
            for (int j = 0; j < max.length; j++) {
                if(min[i] == j&&max[j][0] == i){
                    ans.add(matrix[i][j]);
                }
            }
        }
        return ans;
    }
}
